package com.auto.survey.widget.dialog.optionsdialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.WindowManager
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R


class DynamicOptionsDialog(
    context: Context,
    private val options: List<OptionItem>,
    private val onOptionSelected: (OptionItem) -> Unit
) : Dialog(context, R.style.DialogTheme) {

    private var title: String? = null
    private var isCancelable = true
    private var isCanceledOnTouchOutside = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_options)

        // 设置点击外部和返回键行为
        setCancelable(isCancelable)
        setCanceledOnTouchOutside(isCanceledOnTouchOutside)

        // 初始化标题
        val tvTitle = findViewById<TextView>(R.id.tv_title)
        title?.let { tvTitle.text = it }

        // 初始化 RecyclerView
        val rvOptions = findViewById<RecyclerView>(R.id.rv_options)
        rvOptions.layoutManager = LinearLayoutManager(context)
        rvOptions.adapter = OptionsAdapter(options) { option ->
            onOptionSelected(option)
            dismiss()
        }

        // 优化滚动效果
        rvOptions.isNestedScrollingEnabled = false

        // 设置窗口尺寸（90% 屏幕宽度）
        window?.setLayout(
            (context.resources.displayMetrics.widthPixels * 0.9).toInt(),
            WindowManager.LayoutParams.WRAP_CONTENT
        )
    }

    // 构建器模式
    class Builder(private val context: Context) {
        private var options: List<OptionItem> = emptyList()
        private var title: String? = null
        private var isCancelable = true
        private var isCanceledOnTouchOutside = true
        private var onOptionSelected: (OptionItem) -> Unit = {}

        fun setOptions(options: List<OptionItem>): Builder {
            this.options = options
            return this
        }

        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }

        fun setCancelable(cancelable: Boolean): Builder {
            isCancelable = cancelable
            return this
        }

        fun setCanceledOnTouchOutside(canceledOnTouchOutside: Boolean): Builder {
            isCanceledOnTouchOutside = canceledOnTouchOutside
            return this
        }

        fun setOnOptionSelectedListener(listener: (OptionItem) -> Unit): Builder {
            onOptionSelected = listener
            return this
        }

        fun show(): DynamicOptionsDialog {
            val dialog = DynamicOptionsDialog(context, options, onOptionSelected).apply {
                this.title = this@Builder.title
                this.isCancelable = this@Builder.isCancelable
                this.isCanceledOnTouchOutside = this@Builder.isCanceledOnTouchOutside
            }
            dialog.show()
            return dialog
        }
    }
}